import instance from "./axiosInstance";

// 分页查询虾塘表
export const shrimpPondPage = async (params = {}) => {
  try {
    const response = await instance({
      method: "GET",
      url: "/shrimppond/page",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      params: {
        page: 1,
        pageSize: 10,
        ...params,
      },
    });
    return response.data;
  } catch (error) {
    console.error("虾塘分页查询异常:", error);
    throw error;
  }
};

// 根据id获取虾塘详情
export const getShrimpPondById = async (id) => {
  try {
    const response = await instance({
      method: "GET",
      url: `/shrimppond/${id}`,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    });
    return response.data;
  } catch (error) {
    console.error("获取虾塘详情异常:", error);
    throw error;
  }
};

// 新增虾塘
export const saveShrimpPond = async (data) => {
  try {
    const response = await instance({
      method: "POST",
      url: "/shrimppond",
      headers: {
        "Content-Type": "application/json",
      },
      data: {
        pondName: data.pondName,
        pondArea: data.pondArea,
        pondDepth: data.pondDepth,
        addressBookUpsertRequest: data.addressBookUpsertRequest,
      },
    });
    return response.data;
  } catch (error) {
    console.error("新增虾塘异常:", error);
    throw error;
  }
};

// 编辑虾塘
export const updateShrimpPondById = async (id, data) => {
  try {
    const response = await instance({
      method: "PUT",
      url: `/shrimppond/${id}`,
      headers: {
        "Content-Type": "application/json",
      },
      data: {
        pondName: data.pondName,
        pondArea: data.pondArea,
        pondDepth: data.pondDepth,
        addressBookUpsertRequest: data.addressBookUpsertRequest,
      },
    });
    return response.data;
  } catch (error) {
    console.error("编辑虾塘异常:", error);
    throw error;
  }
};

// 删除虾塘
export const deleteShrimpPondById = async (id) => {
  try {
    const response = await instance({
      method: "DELETE",
      url: `/shrimppond/${id}`,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    });
    return response.data;
  } catch (error) {
    console.error("删除虾塘异常:", error);
    throw error;
  }
};

// 保存虾塘经纬度信息
export const savePondCoordinates = async (pondId, data) => {
  try {
    const response = await instance({
      method: "POST",
      url: `/pondAddressDetail/coordinates/${pondId}`,
      headers: {
        "Content-Type": "application/json",
      },
      data: {
        coordinatesData: data.coordinatesData,
        central: data.central,
      },
    });
    return response.data;
  } catch (error) {
    console.error("保存虾塘经纬度信息异常:", error);
    throw error;
  }
};

// 获取所有虾塘的中心点经纬度
export const getAllCentralCoordinate = async () => {
  try {
    const response = await instance({
      method: "GET",
      url: "/pondAddressDetail/allCentralCoordinate",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    });
    return response.data;
  } catch (error) {
    console.log("获取所有虾塘的中心点经纬度异常:", error);
    throw error;
  }
};

// 根据中心点坐标查询虾塘完整坐标组
export const getPondAddressDetailByCentral = async (central) => {
  try {
    const response = await instance({
      method: "POST",
      url: "/pondAddressDetail/coordinates/central",
      headers: {
        "Content-Type": "application/json",
      },
      data: central,
    });
    return response.data;
  } catch (error) {
    console.log("根据中心点坐标查询虾塘完整坐标组异常:", error);
    throw error;
  }
};
